package com.potenzacd.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.potenzacd.project.mapper.ProductInfoBizFlowInfoMapper;
import com.potenzacd.project.pojo.*;
import com.potenzacd.project.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Cuidongdong
 * @since 2021-01-26
 */
@Service
public class ProductInfoBizFlowInfoService extends ServiceImpl<ProductInfoBizFlowInfoMapper, ProductInfoBizFlowInfo> implements IProductInfoBizFlowInfoService {
    //产品基本信息
    @Autowired
    private IProductInfoService productInfoService;
    //产品历史基本信息
    @Autowired
    private IHisProductInfoService hisProductInfoService;

    //产品业务流程信息
    @Autowired
    private IProductInfoBizFlowInfoService productInfoBizFlowInfoService;
    //产品历史业务流程信息
    @Autowired
    private IHisProductInfoBizFlowInfoService hisProductInfoBizFlowInfoService;

    //产品审核信息
    @Autowired
    private IProductApprovalInfoService productApprovalInfoService;
    //产品历史审核信息
    private IHisProductApprovalInfoService hisProductApprovalInfoService;

    //发送消息
    @Autowired
    private MessageNotificationProductService messageNotificationProductService;

    @Override
    public void save4Draft(ProductInfo productInfo) {
        //1.保存产品基本信息的业务流程信息
        List<ProductInfoBizFlowInfo> productInfoBizFlowInfos = productInfo.getProductInfoBizFlowInfos();
        for (ProductInfoBizFlowInfo productInfoBizFlowInfo:productInfoBizFlowInfos){
            //挂关系
            productInfoBizFlowInfo.setProductInfoCode(productInfo.getCode());
            productInfoBizFlowInfo.setCreateTime(new Date());
            productInfoBizFlowInfo.setUpdateTime(new Date());
        }
        this.productInfoBizFlowInfoService.saveBatch(productInfoBizFlowInfos);
        //将草稿产品的基本信息的业务流程改成草稿状态
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_DRAFT);
        //执行修改
        this.productInfoService.updateById(productInfo);
    }

    @Override
    public void save4Approval(ProductInfo productInfo) {
        //1:保存业务流程信息
        List<ProductInfoBizFlowInfo> productInfoBizFlowInfos = productInfo.getProductInfoBizFlowInfos();
        for(ProductInfoBizFlowInfo productInfoBizFlowInfo: productInfoBizFlowInfos){
            productInfoBizFlowInfo.setUpdateTime(new Date());
            productInfoBizFlowInfo.setCreateTime(new Date());
            productInfoBizFlowInfo.setVersion(1);
            productInfoBizFlowInfo.setProductInfoCode(productInfo.getCode());
        }
        productInfoBizFlowInfoService.saveBatch(productInfoBizFlowInfos);
        //修改产品基本信息状态
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_WAIT_APPROVAL);
        productInfoService.updateById(productInfo);
        //2:保存历史业务流程信息
        ArrayList<HisProductInfoBizFlowInfo> hisProductInfoBizFlowInfos = new ArrayList<>();
        for(ProductInfoBizFlowInfo productInfoBizFlowInfo: productInfoBizFlowInfos){
            HisProductInfoBizFlowInfo hisProductInfoBizFlowInfo = new HisProductInfoBizFlowInfo();
            BeanUtils.copyProperties(productInfoBizFlowInfo,hisProductInfoBizFlowInfo);
            hisProductInfoBizFlowInfo.setHisProductInfoCode(productInfo.getCode());
            hisProductInfoBizFlowInfos.add(hisProductInfoBizFlowInfo);
        }
        hisProductInfoBizFlowInfoService.saveBatch(hisProductInfoBizFlowInfos);
        //3:发送消息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());//发送时间
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setKind(2);//业务流程信息
        messageNotifiProduct.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(productInfo.getCode());
        messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void edit4Draft(ProductInfo productInfo) {
        //1.业务流程修改
        QueryWrapper<ProductInfoBizFlowInfo> productInfoBizFlowInfoQueryWrapper = new QueryWrapper<>();
        productInfoBizFlowInfoQueryWrapper.lambda().eq(ProductInfoBizFlowInfo::getProductInfoCode,productInfo.getCode());
        //先将旧数据删掉然后添加新数据
        this.productInfoBizFlowInfoService.remove(productInfoBizFlowInfoQueryWrapper);
        List<ProductInfoBizFlowInfo> productInfoBizFlowInfos = productInfo.getProductInfoBizFlowInfos();
        for (ProductInfoBizFlowInfo productInfoBizFlowInfo:productInfoBizFlowInfos){
            productInfoBizFlowInfo.setProductInfoCode(productInfo.getCode());
            productInfoBizFlowInfo.setCreateTime(new Date());
            productInfoBizFlowInfo.setUpdateTime(new Date());
        }
        //修改业务流程
        this.productInfoBizFlowInfoService.saveBatch(productInfoBizFlowInfos);
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_DRAFT);
        //2.入库
        this.productInfoService.updateById(productInfo);
    }

    @Override
    public void edit4Approval(ProductInfo productInfo) {
        //1.修改业务流程信息 删除旧资源添加新数据
        QueryWrapper<ProductInfoBizFlowInfo> bizFlowInfoQueryWrapper = new QueryWrapper<>();
        bizFlowInfoQueryWrapper.lambda().eq(ProductInfoBizFlowInfo::getProductInfoCode,productInfo.getCode());
        this.productInfoBizFlowInfoService.remove(bizFlowInfoQueryWrapper);
        //保存业务流程信息
        List<ProductInfoBizFlowInfo> productInfoBizFlowInfos = productInfo.getProductInfoBizFlowInfos();
        for (ProductInfoBizFlowInfo productInfoBizFlowInfo:productInfoBizFlowInfos){
            productInfoBizFlowInfo.setCreateTime(new Date());
            productInfoBizFlowInfo.setUpdateTime(new Date());
            productInfoBizFlowInfo.setVersion(1);
            productInfoBizFlowInfo.setProductInfoCode(productInfo.getCode());
        }
        this.productInfoBizFlowInfoService.saveBatch(productInfoBizFlowInfos);
        //将产品下的业务流程修改为草稿
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_CHANGING_WAIT_APPROVAL);
        this.productInfoService.updateById(productInfo);
        //2.保留历史业务流程信息
        ArrayList<HisProductInfoBizFlowInfo> hisProductInfoBizFlowInfos = new ArrayList<>();
        for (ProductInfoBizFlowInfo productInfoBizFlowInfo:productInfoBizFlowInfos){
            HisProductInfoBizFlowInfo hisProductInfoBizFlowInfo = new HisProductInfoBizFlowInfo();
            BeanUtils.copyProperties(productInfoBizFlowInfo,hisProductInfoBizFlowInfo);
            hisProductInfoBizFlowInfos.add(hisProductInfoBizFlowInfo);
        }
        this.hisProductInfoBizFlowInfoService.saveBatch(hisProductInfoBizFlowInfos);
        //3.发送消息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());//发送时间
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);// 未处理
        messageNotifiProduct.setKind(2);//业务流程信息
        messageNotifiProduct.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(productInfo.getCode());
        this.messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void pass(ProductApprovalInfo productApprovalInfo) {
        //1.先修改产品业务流程的状态为通过
        String productInfoCode = productApprovalInfo.getProductInfoCode();
        productApprovalInfo.setCreateTime(new Date());
        productApprovalInfo.setUpdateTime(new Date());
        productApprovalInfo.setType(2);
        productApprovalInfo.setPass(1);
        this.productApprovalInfoService.save(productApprovalInfo);
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,productInfoCode);
        ProductInfo productInfo = this.productInfoService.getOne(productInfoQueryWrapper);
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_APPROVAL_PASS);
        this.productInfoService.updateById(productInfo);
        //2.保存历史审核信息
        HisProductApprovalInfo hisProductApprovalInfo = new HisProductApprovalInfo();
        BeanUtils.copyProperties(productApprovalInfo,hisProductApprovalInfo);
        hisProductApprovalInfo.setHisProductInfoCode(productInfoCode);
        this.hisProductApprovalInfoService.save(hisProductApprovalInfo);
        //3.发送信息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());//发送时间
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);// 未处理
        messageNotifiProduct.setKind(2);//业务流程信息
        messageNotifiProduct.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(productInfo.getCode());
        this.messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void notPass(ProductApprovalInfo productApprovalInfo) {
        //1.先修改产品业务流程的状态为不通过
        String productInfoCode = productApprovalInfo.getProductInfoCode();
        productApprovalInfo.setCreateTime(new Date());
        productApprovalInfo.setUpdateTime(new Date());
        productApprovalInfo.setType(2);
        productApprovalInfo.setPass(2);
        this.productApprovalInfoService.save(productApprovalInfo);
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,productInfoCode);
        ProductInfo productInfo = this.productInfoService.getOne(productInfoQueryWrapper);
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_APPROVAL_FAIL);
        this.productInfoService.updateById(productInfo);
        //2.保存历史审核信息
        HisProductApprovalInfo hisProductApprovalInfo = new HisProductApprovalInfo();
        BeanUtils.copyProperties(productApprovalInfo,hisProductApprovalInfo);
        hisProductApprovalInfo.setHisProductInfoCode(productInfoCode);
        this.hisProductApprovalInfoService.save(hisProductApprovalInfo);
        //3.发送信息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());//发送时间
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);// 未处理
        messageNotifiProduct.setKind(2);//业务流程信息
        messageNotifiProduct.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(productInfo.getCode());
        this.messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void re4Approval(ProductInfo productInfo) {
        //1.修改业务流程信息 删除旧资源添加新数据
        QueryWrapper<ProductInfoBizFlowInfo> bizFlowInfoQueryWrapper = new QueryWrapper<>();
        bizFlowInfoQueryWrapper.lambda().eq(ProductInfoBizFlowInfo::getProductInfoCode,productInfo.getCode());
        this.productInfoBizFlowInfoService.remove(bizFlowInfoQueryWrapper);
        //保存业务流程信息
        List<ProductInfoBizFlowInfo> productInfoBizFlowInfos = productInfo.getProductInfoBizFlowInfos();
        for (ProductInfoBizFlowInfo productInfoBizFlowInfo:productInfoBizFlowInfos){
            productInfoBizFlowInfo.setCreateTime(new Date());
            productInfoBizFlowInfo.setUpdateTime(new Date());
            productInfoBizFlowInfo.setVersion(1);
            productInfoBizFlowInfo.setProductInfoCode(productInfo.getCode());
        }
        this.productInfoBizFlowInfoService.saveBatch(productInfoBizFlowInfos);
        //将产品下的业务流程修改为草稿
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_CHANGING_WAIT_APPROVAL);
        this.productInfoService.updateById(productInfo);
        //2.保留历史业务流程信息
        ArrayList<HisProductInfoBizFlowInfo> hisProductInfoBizFlowInfos = new ArrayList<>();
        for (ProductInfoBizFlowInfo productInfoBizFlowInfo:productInfoBizFlowInfos){
            HisProductInfoBizFlowInfo hisProductInfoBizFlowInfo = new HisProductInfoBizFlowInfo();
            BeanUtils.copyProperties(productInfoBizFlowInfo,hisProductInfoBizFlowInfo);
            hisProductInfoBizFlowInfos.add(hisProductInfoBizFlowInfo);
            //维护版本
            QueryWrapper<HisProductInfoBizFlowInfo> hisbizFlowInfoQueryWrapper = new QueryWrapper<>();
            //查到版本最大的
            hisbizFlowInfoQueryWrapper.lambda().eq(HisProductInfoBizFlowInfo::getCode,productInfo.getCode()).orderByDesc(HisProductInfoBizFlowInfo::getVersion);//&
            //根据这个区差一条语句
            HisProductInfoBizFlowInfo bizFlowInfoServiceOne = this.hisProductInfoBizFlowInfoService.getOne(hisbizFlowInfoQueryWrapper);
            Integer version = bizFlowInfoServiceOne.getVersion();
            productInfoBizFlowInfo.setVersion(version);
        }



        //3.发送消息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());//发送时间
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);// 未处理
        messageNotifiProduct.setKind(2);//业务流程信息
        messageNotifiProduct.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(productInfo.getCode());
        this.messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void abort(String code) {
        //先将基本信息查出来
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,code);
        ProductInfo productInfo = this.productInfoService.getOne(productInfoQueryWrapper);
        //再查业务流程
        QueryWrapper<ProductInfoBizFlowInfo> productInfoBizFlowInfoQueryWrapper = new QueryWrapper<>();
        productInfoBizFlowInfoQueryWrapper.lambda().eq(ProductInfoBizFlowInfo::getProductInfoCode,productInfo.getCode());
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_ABORT);
        List<ProductInfoBizFlowInfo> productInfoBizFlowInfos = productInfo.getProductInfoBizFlowInfos();
        for (ProductInfoBizFlowInfo productInfoBizFlowInfo:productInfoBizFlowInfos){
            productInfoBizFlowInfo.setProductInfoCode(productInfo.getCode());
        }
        this.productInfoBizFlowInfoService.updateBatchById(productInfoBizFlowInfos);
    }

    @Override
    public ProductInfo detail(String code) {
        //先将基本信息查出来
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,code);
        ProductInfo productInfo = this.productInfoService.getOne(productInfoQueryWrapper);
        //再查业务流程
        QueryWrapper<ProductInfoBizFlowInfo> productInfoBizFlowInfoQueryWrapper = new QueryWrapper<>();
        productInfoBizFlowInfoQueryWrapper.lambda().eq(ProductInfoBizFlowInfo::getProductInfoCode,productInfo.getCode());
        this.productInfoBizFlowInfoService.list(productInfoBizFlowInfoQueryWrapper);

        return productInfo;
    }
}
